package me.sdevil507.platform.controller;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.org.dto.OrgRoleResultDto;
import me.sdevil507.org.po.OrgDeptPo;
import me.sdevil507.org.po.OrgPo;
import me.sdevil507.org.po.QOrgRoleResourceRelPo;
import me.sdevil507.org.repository.OrgDeptRepository;
import me.sdevil507.org.repository.OrgRepository;
import me.sdevil507.org.service.OrgDeptService;
import me.sdevil507.platform.dto.*;
import me.sdevil507.platform.mapper.PlatResourceMapper;
import me.sdevil507.platform.po.PlatDeptPo;
import me.sdevil507.platform.po.PlatResourcePo;
import me.sdevil507.platform.service.PlatResourceService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台资源表[PlatResourcePo]
 *
 * @author sdevil507
 * created on 2020-06-23 18:43:30
 */
@Controller
@RequestMapping("/api/plat/resource")
@Slf4j
public class PlatResourceController {

    @Autowired
    private PlatResourceService platResourceService;

    @Autowired
    private OrgRepository orgRepository;

    @Autowired
    private OrgDeptRepository orgDeptRepository;

    @Autowired
    private PlatResourceMapper platResourceMapper;

    @Autowired
    JPAQueryFactory jpaQueryFactory;

    /**
     * 获取资源树
     *
     * @return 信息列表
     */
    @RequestMapping(value = "/readAll", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultDTO readAll() {
        List<PlatResourceResultDto> list = platResourceMapper.pos2dtos(platResourceService.readAll());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list);
    }

    /**
     * 获取资源模块树(包含按钮)
     *
     * @return 信息列表
     */
    @RequestMapping(value = "/readAllWithBtn", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO readAllWithBtn(@RequestBody OrgRoleResultDto orgRoleResultDto) {

        List<PlatResourcePo> resourcePoList = new ArrayList<>();

        if(orgRoleResultDto.getDeptId() != null){
            OrgDeptPo orgDeptPo = orgDeptRepository.getOne(orgRoleResultDto.getDeptId());
            //TODO 注释掉的原因--新增新的机构 这边角色授权模块 出不来按钮
//            if(orgDeptPo.isItIsOrg()){
//                resourcePoList = orgDeptPo.getOrg().getResources().stream().filter(platResourcePo -> platResourcePo.getType().equals(ComConstant.ModuleType.MODULE_MENU)).collect(Collectors.toList());
//            }else{
                resourcePoList = platResourceService.readAll();
//            }
        }else{
            resourcePoList = platResourceService.readAll();
        }

        List<PlatResourceModuleResultDto> list = platResourceMapper.pos2ModuleDtos(resourcePoList);

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list);
    }


    /**
     * 分页获取信息列表
     *
     * @param dto 聚合参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAllByPage", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("plat:module:view")
    public ApiResultDTO readAllByPage(@RequestBody PlatResourceMergeParamsDto dto) {
        Page<PlatResourcePo> pagePos = platResourceService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<PlatResourceSimpleResultDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<PlatResourceSimpleResultDto, PlatResourcePo>() {
            @Override
            public List<PlatResourceSimpleResultDto> exec(List<PlatResourcePo> list) {
                return platResourceMapper.pos2SimpleDtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 创建时获取模块编码
     *
     * @param dto 入参
     * @return 模块编码
     */
    @RequestMapping(value = "/getModuleCode", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO getModuleCode(@RequestBody PlatResourceParamsDto dto) {
        String moduleCode = platResourceService.getModuleCode(dto.getParentId());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), moduleCode);
    }

    /**
     * 创建
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("plat:module:add")
    public ApiResultDTO create(@RequestBody PlatResourceParamsDto dto) {
        platResourceService.create(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 更新
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("plat:module:edit")
    public ApiResultDTO update(@RequestBody PlatResourceParamsDto dto) {
        platResourceService.update(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("plat:module:delete")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO delete(@RequestBody PlatResourceParamsDto dto) {
        platResourceService.delete(dto);
        //删除角色与资源的对应信息
        QOrgRoleResourceRelPo qOrgRoleResourceRelPo = QOrgRoleResourceRelPo.orgRoleResourceRelPo;
        jpaQueryFactory.delete(qOrgRoleResourceRelPo).where(qOrgRoleResourceRelPo.resource.id.eq(dto.getId())).execute();

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }
}